<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Model : kehadiran
 * di Buat oleh Diar PHP Generator
 * Update List untuk grid karena program generatorku lom sempurna ya hehehehehe */

class modelkehadiran extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getArrayListkehadiran() { /* spertinya perlu lock table */
        $xBuffResul = array();
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "tanggal," .
                "jam," .
                "keterangan," .
                "tglupdate," .
                "tglinsert," .
                "idpegawai" .
                " FROM kehadiran   order by idx ASC ";
        $query = $this->db->query($xStr);
        foreach ($query->result() as $row) {
            $xBuffResul[$row->idx] = $row->idcustomer;
        }
        return $xBuffResul;
    }

    function getListkehadiran($xAwal, $xLimit, $xSearch = '') {
        if (!empty($xSearch)) {
            $xSearch = "Where idcustomer like '%" . $xSearch . "%'";
        }
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "tanggal," .
                "jam," .
                "keterangan," .
                "tglupdate," .
                "tglinsert," .
                "idpegawai" .
                " FROM kehadiran $xSearch order by idx DESC limit " . $xAwal . "," . $xLimit;
        $query = $this->db->query($xStr);
        return $query;
    }

    function getDetailkehadiran($xidx) {
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "tanggal," .
                "jam," .
                "keterangan," .
                "tglupdate," .
                "tglinsert," .
                "idpegawai" .
                " FROM kehadiran  WHERE idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    function getLastIndexkehadiran() { /* spertinya perlu lock table */
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "tanggal," .
                "jam," .
                "keterangan," .
                "tglupdate," .
                "tglinsert," .
                "idpegawai" .
                " FROM kehadiran order by idx DESC limit 1 ";
        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    function getLastIndexkehadiranByIdCust($xidcust) { /* spertinya perlu lock table */
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "tanggal," .
                "jam," .
                "keterangan," .
                "tglupdate," .
                "tglinsert," .
                "idpegawai" .
                " FROM kehadiran WHERE idcustomer = '".$xidcust."' order by idx DESC limit 1 ";
        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }
    
    Function setInsertkehadiran($xidx, $xidcustomer, $xtanggal, $xjam, $xketerangan, $xtglupdate, $xtglinsert, $xidpegawai) {
        $xStr = " INSERT INTO kehadiran( " .
                "idx," .
                "idcustomer," .
                "tanggal," .
                "jam," .
                "keterangan," .
                "tglupdate," .
                "tglinsert," .
                "idpegawai) VALUES('" . $xidx . "','" . $xidcustomer . "',current_date,current_time,'" . $xketerangan . "',now(),now(),'" . $xidpegawai . "')";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    Function setUpdatekehadiran($xidx, $xidcustomer, $xtanggal, $xjam, $xketerangan, $xtglupdate, $xtglinsert, $xidpegawai) {
        $xStr = " UPDATE kehadiran SET " .
                "idx='" . $xidx . "'," .
                "idcustomer='" . $xidcustomer . "'," .
                "tanggal='" . $xtanggal . "'," .
                "jam='" . $xjam . "'," .
                "keterangan='" . $xketerangan . "'," .
                "tglupdate='" . $xtglupdate . "'," .
                "tglinsert='" . $xtglinsert . "'," .
                "idpegawai='" . $xidpegawai . "' WHERE idx = '" . $xidx . "'";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    function setDeletekehadiran($xidx) {
        $xStr = " DELETE FROM kehadiran WHERE kehadiran.idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $this->setInsertLogDeletekehadiran($xidx);
    }

    function setInsertLogDeletekehadiran($xidx) {
        $xidpegawai = $this->session->userdata('idpegawai');
        $xStr = "insert into logdelrecord(idxhapus,nmtable,tgllog,ideksekusi) values($xidx,'kehadiran',now(),$xidpegawai)";
        $query = $this->db->query($xStr);
    }

}

?>